package com.vcredit.creditcat.service.speedloan.sd;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.creditcat.dto.speedloan.CheckCustResultDto;
import com.vcredit.creditcat.model.AuthIdentity;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.model.CustAccount;
import com.vcredit.creditcat.model.ProductCheckcustItem;
import com.vcredit.creditcat.service.AuthIdentityService;
import com.vcredit.creditcat.service.CustAccountService;
import com.vcredit.creditcat.service.speedloan.CommonProductCheckCustService;
import com.vcredit.creditcat.service.speedloan.ProductCheckCustServiceAdaptor;
import com.vcredit.creditcat.vo.product.sd.SDQueryParamVo;
import com.vcredit.creditcat.vo.product.sd.SDQueryResultVo;
import com.vcredit.creditcat.vo.product.sd.SDResultVo;
import com.vcredit.framework.util.HttpRequest;
import com.vcredit.framework.util.JacksonUtil;

@Service(ProductCheckCustServiceAdaptor.BEANNAME_PREFIX + 3)
public class SDCheckCustService extends CommonProductCheckCustService{
	private static final Logger LOG = LoggerFactory.getLogger(SDCheckCustService.class);
	
	@Autowired
	private AuthIdentityService authIdentityService;
	@Autowired
	private CustAccountService custAccountService;
	
	@Value("${product.sd.queryUrl}")
	private String queryUrl;
	
	@Override
	protected CheckCustResultDto handlerCheckItem(
			ProductCheckcustItem checkItem, int itemType, Cust cust,
			Long productId) {
		if(null != cust && null == cust.getCustId()) {
			return CheckCustResultDto.success();
		}
		int checkItemId = checkItem.getCheckItemId();
		if(itemType == 0) { // 原始认证项
			switch (checkItemId) {
			case 1:
				try {
					return this.check(cust);
				} catch (Exception e) {  //接口异常默认通过，防止网络等原因影响后续流程
					return CheckCustResultDto.success();
				} 
			default:
				break;
			}
		}
		
		return CheckCustResultDto.success();
	}

	private CheckCustResultDto check(Cust cust) throws Exception {
		AuthIdentity authIdentity = authIdentityService.findByProcessId(cust.getLastProccessId());
		CustAccount custAccount = custAccountService.findByCustId(cust.getCustId());
		
		SDQueryParamVo queryParam = new SDQueryParamVo(authIdentity.getCitizenId(), authIdentity.getName(),custAccount.getUserName());
		String queryParamStr = JacksonUtil.Obj2Json(queryParam);
		LOG.info("闪贷可交单查询状态入参："+queryParamStr);
		String queryResultStr = HttpRequest.doJson(queryUrl, queryParamStr);
		LOG.info("闪贷可交单查询状态出参："+queryResultStr);
		SDResultVo<SDQueryResultVo> result = JacksonUtil.Json2Obj(queryResultStr, new TypeReference<SDResultVo<SDQueryResultVo>>(){});
		if("1".equals(result.getStatus()) && result.getData() != null) {
			Integer orderState = result.getData().getOrderState();
			if(orderState != 0) {
				return CheckCustResultDto.fail("黑名单|账号已存在");
			}
		}
		return CheckCustResultDto.success();
	}

}
